<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="表名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入表名" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="queryParams.description" placeholder="请输入描述" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button v-hasPermi="['tool:code-generate:list']" type="primary" icon="el-icon-search" size="small"
                   @click="handleQuery">搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        <el-button :disabled="multiple" type="primary" plain icon="el-icon-download" size="small"
                   @click="handleGenerate"
                   v-hasPermi="['tool:code-generate:gen']">批量生成
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="表名" prop="name" align="center"/>
      <el-table-column label="描述" prop="description" align="center"/>
      <el-table-column label="创建时间" prop="createTime" align="center"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-download" @click="handleGenerate(scope.row)"
                     v-hasPermi="['tool:code-generate:gen']">生成
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <Pagination v-show="total>0" :total="total" :page.sync="queryParams.currentPage" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
  </div>
</template>

<script>
  import {listTable} from "@/api/tool/code-generate";
  import Pagination from "@/components/Pagination"

  export default {
    name: "Generate",
    components: {
      Pagination
    },
    data() {
      return {
        loading: true,
        names: [],
        multiple: true,
        total: 0,
        tableList: [],
        queryParams: {
          currentPage: 1,
          pageSize: 10,
          name: undefined,
          description: undefined
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      getList() {
        this.loading = true;
        listTable(this.queryParams).then(response => {
          this.tableList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        });
      },
      handleQuery() {
        this.queryParams.currentPage = 1;
        this.getList();
      },
      resetQuery() {
        this.queryParams = {}
        this.handleQuery();
      },
      handleSelectionChange(selection) {
        this.names = selection.map(item => item.name)
        this.multiple = !selection.length
      },
      handleGenerate(row) {
        const names = row.name || this.names;
        this.zip('tool/code-generate?names=' + names, 'code.zip')
      }
    }
  };
</script>
